import axios from "axios";
import { ElMessage } from "element-plus";

const instance = axios.create({
    baseURL: "http://localhost:5000/api/v1/",
    timeout: 4000,
    withCredentials: true
});

instance.interceptors.request.use(
    (config) => {
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

instance.interceptors.response.use(
    (res) => {
        if (res.data.code == "-1") {
            ElMessage({
                message: res.data.message,
                type: "error"
            });
            return false;
        }
        return res.data.data;
    },
    (error) => {
        ElMessage({
            message: error.message,
            type: "error"
        });
        return Promise.reject(error);
    }
);

export default instance;
